using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Core;
using System.Drawing;
using System.IO;

namespace System.Web.Core
{
    public class UserAvatar : PlaceHolder
    {
        #region Private Members

        private int defaultWidth = 80;
        private int defaultHeight = 100;
        private User user;

        #endregion

        #region Public Properties

        public User User
        {
            get
            {
                if (user == null)
                {
                    if (UserId > 0)
                    {
                        user = UserManager.GetUser(UserId);
                    }
                    if (user == null && !string.IsNullOrEmpty(MemberName))
                    {
                        Member member = MemberManager.GetMember(MemberName);
                        if (member != null)
                        {
                            user = UserManager.GetUser(member.MemberId);
                        }
                    }
                    if (user == null)
                    {
                        user = (User)HttpContext.Current.User;
                    }
                }
                return user;
            }
            set
            {
                user = value;
                if (user != null)
                {
                    ViewState["UserId"] = user.EntityId.Value;
                }
                else
                {
                    ViewState.Remove("UserId");
                }
            }
        }
        public int UserId
        {
            get
            {
                object state = ViewState["UserId"];
                if (state != null)
                {
                    return (int)state;
                }
                return 0;
            }
            set
            {
                ViewState["UserId"] = value;
            }
        }
        public string MemberName
        {
            get
            {
                object state = ViewState["MemberName"];
                if (state != null)
                {
                    return state as string;
                }
                return string.Empty;
            }
            set
            {
                ViewState["MemberName"] = value;
            }
        }
        public string AvatarUrl
        {
            get
            {
                object state = ViewState["AvatarUrl"];
                if (state != null)
                {
                    return state as string;
                }
                return "userAvatar.aspx";
            }
            set
            {
                ViewState["AvatarUrl"] = value;
            }
        }
        public string AnonymousAvatarUrl
        {
            get
            {
                string state = (string)ViewState["AnonymousAvatarUrl"];
                if (state != null)
                {
                    return state;
                }
                return Configuration.Instance.UserDefaultAvatarPath;
            }
            set
            {
                ViewState["AnonymousAvatarUrl"] = value;
            }
        }
        public int Border
        {
            get
            {
                object state = ViewState["Border"];
                if (state != null)
                {
                    return (int)state;
                }
                return 0;
            }
            set
            {
                ViewState["Border"] = value;
            }
        }
        public int Height
        {
            get
            {
                object state = ViewState["Height"];
                if (state != null)
                {
                    return (int)state;
                }
                return 0;
            }
            set
            {
                ViewState["Height"] = value;
            }
        }
        public int Width
        {
            get
            {
                object state = ViewState["Width"];
                if (state != null)
                {
                    return (int)state;
                }
                return 0;
            }
            set
            {
                ViewState["Width"] = value;
            }
        }
        public bool IsCustomSize
        {
            get
            {
                return this.Height > 0 && this.Width > 0;
            }
        }
        public bool UseRealSize
        {
            get
            {
                object state = ViewState["UseRealSize"];
                if (state != null)
                {
                    return (bool)state;
                }
                return true;
            }
            set
            {
                ViewState["UseRealSize"] = value;
            }
        }
        public string BackColor
        {
            get
            {
                string state = (string)ViewState["BackColor"];
                if (state != null)
                {
                    return state;
                }
                return "FFFFFF";
            }
            set
            {
                ViewState["BackColor"] = value;
            }
        }

        #endregion

        #region Override Methods

        protected override void Render(HtmlTextWriter writer)
        {
            HtmlImage image = new HtmlImage();

            if (IsCustomSize)
            {
                Avatar userAvatar = UserManager.GetUserAvatar(User);
                if (userAvatar != null && userAvatar.Content != null)
                {
                    SetImgSize(image, System.Drawing.Image.FromStream(new MemoryStream(userAvatar.Content)));
                }
            }
            else
            {
                image.Width = this.defaultWidth;
                image.Height = this.defaultHeight;
            }

            string parameters = string.Format(
                "?{0}={1}&{2}={3}&{4}={5}&{6}={7}&{8}={9}&{10}={11}",
                ParameterName.PictureWidth.ToString(),
                image.Width,
                ParameterName.PictureHeight.ToString(),
                image.Height,
                ParameterName.UseRealSize.ToString(),
                UseRealSize,
                ParameterName.BackColor.ToString(),
                BackColor,
                ParameterName.UserId.ToString(),
                User.EntityId.Value,
                ParameterName.Tags.ToString(),
                Guid.NewGuid()
                );

            image.Src = Globals.ApplicationPath + "/" + AvatarUrl.TrimStart('/') + parameters;
            image.Border = this.Border;
            image.RenderControl(writer);
        }

        #endregion

        #region Private Methods

        private void SetImgSize(HtmlImage img, System.Drawing.Image avatar)
        {
            if (!UseRealSize)
            {
                img.Width = Width;
                img.Height = Height;
            }
            else
            {
                Size newSize = Globals.GetNewSize(Width, Height, avatar.Width, avatar.Height);
                img.Width = newSize.Width;
                img.Height = newSize.Height;
            }
        }

        #endregion
    }
}